package com.mrdeer.iot.controller;

import com.mrdeer.common.JSONResult;
import com.mrdeer.iot.CommandExecutor;
import com.mrdeer.iot.bean.command.Instruction;
import com.mrdeer.iot.constant.FrameConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 消息指令相关
 *
 * @Author: xie jianchu
 */
@Slf4j
@RestController
@Api(tags = "烘干设备指令相关接口")
@RequestMapping("/mrdeer/command/dry")
@Transactional
public class CommandDryController {

    @Resource
    private CommandExecutor commandExecutor;

    //1.实时获取设备状态（在收到心跳指令的时候通过websocket回传页面）
    //2.整机指令 1、主动查询 2、静音开启/关闭 3、语音播报控制
    //3.洗涤仓命令 1、授权/解除授权  2、洗涤仓启动/暂停  3、洗涤仓门开关  4、程序设定  5、终止当前运行状态,回到待机状态
    //4.烘干仓指令 1、授权/解除授权  2、烘干仓启动/暂停 3、程序设定  4、烘干仓终止当前运行状态，回到待机状态

    @PostMapping("/authorize/{address}/{status}/{dry}")
    @ApiOperation(value = "根据设备地址发送授权指令,dry为烘干设备1或者2")
    public JSONResult authorize(@PathVariable String address, @PathVariable String status, @PathVariable String dry) {
        if (dry.equals("1")) {
            if (status.equals("1")) {
                //授权
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_1.authorization);
            } else if (status.equals("0")) {
                //解除授权
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_1.deauthorize);
            } else {
                //无效命令
                return JSONResult.errorMsg("未执行任何指令");
            }
        } else if (dry.equals("2")) {
            if (status.equals("1")) {
                //授权
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_2.authorization);
            } else if (status.equals("0")) {
                //解除授权
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_2.deauthorize);
            } else {
                //无效命令
                return JSONResult.errorMsg("未执行任何指令");
            }
        } else {
            //无效命令
            return JSONResult.errorMsg("未找到对应的烘干仓");
        }
        return JSONResult.ok();
    }

    @PostMapping("/start/{address}/{status}/{dry}")
    @ApiOperation(value = "根据设备地址发送开启暂停指令")
    public JSONResult startDry(@PathVariable String address, @PathVariable String status, @PathVariable String dry) {
        if (dry.equals("1")) {
            if (status.equals("1")) {
                //开始烘干
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_1.dry_start);
            } else if (status.equals("0")) {
                //暂停烘干
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_1.dry_stop);
            } else {
                //无效命令
                return JSONResult.errorMsg("未执行任何指令");
            }
        } else if (dry.equals("2")) {
            if (status.equals("1")) {
                //开始烘干
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_2.dry_start);
            } else if (status.equals("0")) {
                //暂停烘干
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_2.dry_stop);
            } else {
                //无效命令
                return JSONResult.errorMsg("未执行任何指令");
            }
        } else {
            return JSONResult.errorMsg("设备dry未识别");
        }
        return JSONResult.ok();
    }

    @PostMapping("/program/{address}/{program}/{dry}")
    @ApiOperation(value = "根据设备地址发送程序指令")
    public JSONResult setProgram(@PathVariable String address, @PathVariable String program, @PathVariable String dry) {
        if (dry.equals("1")) {
            if (program.equals("1")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_1_null);
            } else if (program.equals("2")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_1_auto);
            } else if (program.equals("3")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_1_sterilize);
            }
        } else if (dry.equals("2")) {
            if (program.equals("1")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_2_null);
            } else if (program.equals("2")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_2_auto);
            } else if (program.equals("3")) {
                commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.program_set.drying_2_sterilize);
            }
        }
        return JSONResult.ok();
    }

    @PostMapping("/stop/{address}/{dry}")
    @ApiOperation(value = "根据设备地址发送强制暂停，回到待机状态")
    public JSONResult stop(@PathVariable String address, @PathVariable String dry) {
        if (dry.equals("1")) {
            commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_1.dry_await);
        } else if (dry.equals("2")) {
            commandExecutor.processCommand(address, FrameConst.control, Instruction.ControlFrame.drying_2.dry_await);
        }
        return JSONResult.ok();
    }
}
